﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{

    internal class UISubCategory
    {
        private ArbolandiaDBDataContext _db;

        public UISubCategory()
        {
            _db = new ArbolandiaDBDataContext();
        }

        internal UISubcategoryDetail GetSubCategoryDetail(int idSubCategory)
        {
            return CompiledQuerys.GetSubCategoryDetail(_db, idSubCategory);
        }

        internal List<UIProductResume> GetProductsBySubCategory(int idSubCategory)
        {
            //Obtenemos Lista de Productos
            var listOfProducts = CompiledQuerys.GetProductsBySubCategory(_db, idSubCategory);
            //Obtenemos lista de imagenes de cada producto.
            foreach (var item in listOfProducts)
            {
                var listPreferences = from p in _db.spGetProductPreferences(item.Id)
                                      select new
                                      {
                                          Name = p.Name,
                                          Description = p.Description,
                                          Order = p.Order,
                                          ImageUrl = p.url
                                      };
                item.ProductPreferences = new List<UIProductPreference>();
                foreach (var p in listPreferences)
                {
                    item.ProductPreferences.Add(new UIProductPreference()
                    {
                        Name = p.Name,
                        Description = p.Description,
                        Order = (short)p.Order,
                        ImageUrl = p.ImageUrl
                    });
                }
            }

            return listOfProducts;
        }

        internal List<UIProductVariousResume> GetProductsVariousBySubCategory(int idSubCategory)
        {
            var res = new List<UIProductVariousResume>();
            //Obtenemos Lista de Productos
            var listOfProducts = from p in _db.spGetProductsBySubCategoryUI(idSubCategory)
                                 select p;
            //Obtenemos lista de imagenes de cada producto.
            foreach (var item in listOfProducts)
            {
                res.Add(new UIProductVariousResume()
                {
                    Id = item.Id,
                    ImageUrl = item.PictureUrl,
                    LowerPrice = item.LowerPrice,
                    Name = item.Name,
                    ProductDetailId = item.ProductDetailId,
                    ShortDescription = item.ShortDescription
                });
            }

            return res;
        }

        internal List<UISubcategoryResume> GetSubcategorys(Int32 categoryId)
        {

            return CompiledQuerys.GetSubCategory(_db, categoryId);
            //return (from p in _db.spGetSubCategory(categoryId)
            //        select new UISubcategoryResume()
            //        {
            //            Id = p.Id,
            //            IdCategory = p.IdCategory,
            //            ImageUrl = p.ImageUrl,
            //            LowerPrice = p.LowerPrice,
            //            Name = p.Name,
            //            TotalProducts = p.TotalProducts
            //        }
            // ).ToList();
        }

    }
}
